<?php

class AccountController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'login';

    public function actions()
    {
        return array(
            // captcha action renders the CAPTCHA image displayed on the contact page
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'foreColor' => 0x0080D3,
                'backColor' => 0xFFFFFF,
                'maxLength' => 4,       // 验证码最大长度
                'minLength' => 4,        // 验证码最小长度
                'width' => 100,
                'height' => 32,
                'padding' => -5,
                'transparent' => true,
                'testLimit' => 1,
                'offset' => -1,
                'fontFile' => dirname(YII::app()->basePath) . '/css/DamageInc.ttf',
            ),
            // page action renders "static" pages stored under 'protected/views/site/pages'
            // They can be accessed via: index.php?r=site/page&view=FileName
            'page' => array(
                'class' => 'CViewAction',
            ),
        );
    }

    public function actionLogin()
    {

        $this->layout = 'guest';
        if (!Yii::app()->user->isGuest) {
            $this->redirect(array('/' . Yii::app()->defaultController));
        }
        $model = new FormLogin;
        if (isset($_POST['FormLogin'])) {
            $model->attributes = $_POST['FormLogin'];
            if ($model->validate() && $model->login()) {
                if (Yii::app()->user->returnUrl == '') {
                    $this->redirect(array('/' . Yii::app()->defaultController));
                } else {
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
        }
        $this->render('login', array('model' => $model));
    }

    public function actionLogout()
    {
        Yii::app()->user->logout();
        $this->redirect(Yii::app()->request->hostInfo . Yii::app()->homeUrl);
    }

    public function actionProfile()
    {
        $uid = Yii::app()->user->getState('user_id');
        if ($uid) {
            $user = User::model()->findByPk($uid);
            $currentPassword = $user->password;
            $user->password = '';
            if (isset($_POST['User'])) {
                $passwordChange = false;
                $newPassword = '';
                $newPasswordConfirm = '';
                if (isset($_POST['User']['mobile'])) {
                    $user->mobile = $_POST['User']['mobile'];
                }
                if (isset($_POST['User']['company'])) {
                    $user->company = $_POST['User']['company'];
                }
                if (isset($_POST['User']['remark'])) {
                    $user->remark = $_POST['User']['remark'];
                }
                if (isset($_POST['User']['password'])) {
                    $user->password = $_POST['User']['password'];
                }
                if (isset($_POST['User']['passwordConfirm'])) {
                    $user->passwordConfirm = $_POST['User']['passwordConfirm'];
                }
                if (($user->password == '') && ($user->passwordConfirm == '')) {
                    $user->password = $currentPassword;
                    $user->passwordConfirm = $currentPassword;
                } else {
                    $passwordChange = true;
                    $newPassword = $user->password;
                    $newPasswordConfirm = $user->passwordConfirm;
                    $user->password = md5($user->pwd_salt . $user->password);
                    $user->passwordConfirm = md5($user->pwd_salt . $user->passwordConfirm);
                }
                if ($user->save()) {
                    $user->password = '';
                    $user->passwordConfirm = '';
                    if ($passwordChange) {
                        Yii::app()->user->setFlash('alert-success', '账号信息及密码已修改，下次登录请使用新密码。');
                    } else {
                        Yii::app()->user->setFlash('alert-success', '账号信息已修改。');
                    }
                } else {
                    $user->password = $newPassword;
                    $user->passwordConfirm = $newPasswordConfirm;
                }
            }
            $this->render('profile', array(
                'user' => $user,
            ));
        } else {
            $this->redirect(array($this->defaultAction));
        }
    }

    // Uncomment the following methods and override them if needed
    /*
    public function filters()
    {
        // return the filter configuration for this controller, e.g.:
        return array(
            'inlineFilterName',
            array(
                'class'=>'path.to.FilterClass',
                'propertyName'=>'propertyValue',
            ),
        );
    }

    */
}